import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ResData, Status } from 'src/response/res-data.interface';
import { Repository } from 'typeorm';
import { CreateMatterDto } from './dto/create-matter.dto';
import { UpdateMatterDto } from './dto/update-matter.dto';
import Matter from './entities/matter.entity';

@Injectable()
export class MatterService {
  constructor(@InjectRepository(Matter) private matterRepository: Repository<Matter>) { }

  async create(createMatterDto: CreateMatterDto) {
    let resdata: ResData;
    try {
      const result = await this.matterRepository.save(createMatterDto);
      resdata = {
        code: Status.SUCCESS,
        msg: '保存成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async findAll(querys) {
    let resdata: ResData;
    try {
      const { startTime, endTime, matterName } = querys;
      let sql = this.matterRepository.createQueryBuilder("matter");
      sql.leftJoinAndSelect('matter.customFlow', "customFlow")
      sql.leftJoinAndSelect('matter.dept', "dept")
      if (matterName) {
        sql.where('matter.matterName like :matterName', { matterName: `%${matterName}%` });
      }
      if (startTime) {
        sql.andWhere('matter.createdDate between :startTime and :endTime', { startTime, endTime })
      }
      const result = await sql.skip(querys.skip).take(querys.take).orderBy("matter.updatedDate", "DESC").getManyAndCount()

      const [list, total] = result
      resdata = {
        code: Status.SUCCESS,
        msg: '查询成功',
        content: {
          result: list,
          total
        }
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async findOne(id: string) {
    let resdata: ResData;
    try {
      const result = await this.matterRepository.findOne(id);
      resdata = {
        code: Status.SUCCESS,
        msg: '查询成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async update(id: string, updateMatterDto: UpdateMatterDto) {
    let resdata: ResData;
    try {
      const result = await this.matterRepository.update(id, updateMatterDto);
      resdata = {
        code: Status.SUCCESS,
        msg: '更新成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async remove(id: string) {
    let resdata: ResData;
    try {
      const result = await this.matterRepository.delete(id);
      resdata = {
        code: Status.SUCCESS,
        msg: '删除成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }
}
